package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Fitting;
import com.windfone.model.Fittingtype;
import com.windfone.model.Fittingtypebase;
import com.windfone.service.FittingService;

public class FittingAction extends ActionSupport {
	private FittingService fittingService;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private List<Fitting> fittingList;
	private List<Fittingtype> fittingTypeList;
	private Fittingtype fittingtype;
	private Fitting fitting;
	private String typeName;
	private String parameter;
	private String otherProperties;
	private String marketTime;
	private String content;
	private double price;
	private String priceStr;
	private String imagepath;
	private String bigimagepath;
	private String fittingTypeName;
	private int basetypeId;
	private int langId;
	private int fittingTypeId;
	private String standar;
	private String memo;
	private String service;
	private int fittingId;
	
	public int getBeginRecord() {
		return beginRecord;
	}

	public List<Fittingtype> getFittingTypeList() {
		return fittingTypeList;
	}

	public Fittingtype getFittingtype() {
		return fittingtype;
	}

	public void setFittingTypeList(List<Fittingtype> fittingTypeList) {
		this.fittingTypeList = fittingTypeList;
	}

	public void setFittingtype(Fittingtype fittingtype) {
		this.fittingtype = fittingtype;
	}

	public String getImagepath() {
		return imagepath;
	}

	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}

	public String getFittingTypeName() {
		return fittingTypeName;
	}

	public int getBasetypeId() {
		return basetypeId;
	}

	public String getStandar() {
		return standar;
	}

	public String getMemo() {
		return memo;
	}

	public String getService() {
		return service;
	}

	public void setStandar(String standar) {
		this.standar = standar;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public void setService(String service) {
		this.service = service;
	}

	public int getLangId() {
		return langId;
	}

	public String getBigimagepath() {
		return bigimagepath;
	}

	public void setBigimagepath(String bigimagepath) {
		this.bigimagepath = bigimagepath;
	}

	public int getFittingTypeId() {
		return fittingTypeId;
	}

	public void setFittingTypeName(String fittingTypeName) {
		this.fittingTypeName = fittingTypeName;
	}

	public void setBasetypeId(int basetypeId) {
		this.basetypeId = basetypeId;
	}

	public void setLangId(int langId) {
		this.langId = langId;
	}

	public void setFittingTypeId(int fittingTypeId) {
		this.fittingTypeId = fittingTypeId;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public List<Fitting> getFittingList() {
		return fittingList;
	}

	public Fitting getFitting() {
		return fitting;
	}

	public String getTypeName() {
		return typeName;
	}

	public String getParameter() {
		return parameter;
	}

	public String getOtherProperties() {
		return otherProperties;
	}

	public String getMarketTime() {
		return marketTime;
	}

	public String getContent() {
		return content;
	}

	public double getPrice() {
		return price;
	}

	public String getPriceStr() {
		return priceStr;
	}

	public int getFittingId() {
		return fittingId;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public void setFittingList(List<Fitting> fittingList) {
		this.fittingList = fittingList;
	}

	public void setFitting(Fitting fitting) {
		this.fitting = fitting;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	public void setOtherProperties(String otherProperties) {
		this.otherProperties = otherProperties;
	}

	public void setMarketTime(String marketTime) {
		this.marketTime = marketTime;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public void setPriceStr(String priceStr) {
		this.priceStr = priceStr;
	}

	public void setFittingId(int fittingId) {
		this.fittingId = fittingId;
	}

	public FittingService getFittingService() {
		return fittingService;
	}

	public void setFittingService(FittingService fittingService) {
		this.fittingService = fittingService;
	}
	
	public String fitting() throws Exception {
		fittingtypebases = fittingService.getAllFittingtypebase();
		return this.SUCCESS;
	}
	
	public String addFitting() throws Exception {
		Fitting fit = new Fitting();
		fit.setTypeName(typeName);
		fit.setStandar(standar);
		fit.setMemo(memo);
		fit.setService(service);
		fit.setParameter(parameter);
		fit.setBigimagepath(bigimagepath);
		fit.setOtherProperties(otherProperties);
		fit.setMarketTime(marketTime);
		fit.setContent(content);
		if ( priceStr!= null )
			price = Double.parseDouble(priceStr);
		fit.setPrice(price);
		fit.setFittingTypeBaseId(basetypeId);
		fit.setFittingTypeId(fittingTypeId);
		fit.setImagepath(imagepath);	
		fittingService.insertFitting(fit);
		return this.SUCCESS;
	}
	
	public String deleteFitting() throws Exception {
		fittingService.deleteFitting(fittingId);
		return this.SUCCESS;
	}
	
	private int  fittingtypebaseIdQ=-1;
	private int  fittingTypeIdQ=-1;
	private String beginDate;
	private String endDate;
	private List<Fittingtypebase> fittingtypebases;
	public String fittingList() throws Exception {
		fittingtypebases = fittingService.getAllFittingtypebase();
		String whereCondition = "";
		if ( fittingtypebaseIdQ !=-1){
			whereCondition = "  AND b.id=" + fittingtypebaseIdQ;
		}
		if ( fittingTypeIdQ !=-1){
			whereCondition = "  AND f.id=" + fittingTypeIdQ;
		}
		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND f.createTime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND f.createTime<='"+ endDate  +"'  ";
		}		
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = fittingService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		fittingList = fittingService.getAllFitting(beginRecord,whereCondition);
		fittingTypeIdQ = -1;
		return SUCCESS;
	}
	public int getFittingTypeIdQ() {
		return fittingTypeIdQ;
	}

	public void setFittingTypeIdQ(int fittingTypeIdQ) {
		this.fittingTypeIdQ = fittingTypeIdQ;
	}
	public int getFittingtypebaseIdQ() {
		return fittingtypebaseIdQ;
	}

	public void setFittingtypebaseIdQ(int fittingtypebaseIdQ) {
		this.fittingtypebaseIdQ = fittingtypebaseIdQ;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public String getEndDate() {
		return endDate;
	}
 

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<Fittingtypebase> getFittingtypebases() {
		return fittingtypebases;
	}

	public void setFittingtypebases(List<Fittingtypebase> fittingtypebases) {
		this.fittingtypebases = fittingtypebases;
	}

	public String beforUpdateFitting() throws Exception {
		fitting = fittingService.getFittingById(fittingId).get(0);
		fittingTypeList = fittingService.getFittingTypeByBaseId(fitting.getFittingTypeBaseId());		
		return this.SUCCESS;
	}
	public String updateFitting() throws Exception {
		Fitting fit = new Fitting();
		fit.setStandar(standar);
		fit.setMemo(memo);
		fit.setService(service);
		fit.setTypeName(typeName);	
		fit.setBigimagepath(bigimagepath);
		fit.setParameter(parameter);
		fit.setOtherProperties(otherProperties);
		fit.setMarketTime(marketTime);
		fit.setContent(content);
		if ( priceStr!= null )
			price = Double.parseDouble(priceStr);
		fit.setPrice(price);
		fit.setId(fittingId);
		fit.setFittingTypeBaseId(basetypeId);
		fit.setFittingTypeId(fittingTypeId);		
		fit.setImagepath(imagepath);		
		fittingService.updateFitting(fit);
		return this.SUCCESS;
	}
	 
	public String fittingType() throws Exception {
		fittingtypebases = fittingService.getAllFittingtypebase();
		return this.SUCCESS;
	}
	
	public String addFittingType() throws Exception {
		Fittingtype fittype = new Fittingtype();
		fittype.setBasetypeId(basetypeId);
		fittype.setLangId(langId);
		fittype.setName(fittingTypeName);
		fittingService.insertFittingType(fittype);
		return this.SUCCESS;
	}
	
	public String deleteFittingType() throws Exception {
		fittingService.deleteFittingType(fittingTypeId);
		return this.SUCCESS;
	}
	
	public String fittingTypeList() throws Exception {
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = fittingService.getAllTypeRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		fittingTypeList = fittingService.getAllFittingType(beginRecord);		
		return this.SUCCESS;
	}
	
	public String beforUpdateFittingType() throws Exception {
		fittingtype = fittingService.getFittingTypeById(fittingTypeId).get(0);
		fittingtypebases = fittingService.getAllFittingtypebase();
		return this.SUCCESS;
	}
	
	public String updateFittingType() throws Exception {
		Fittingtype fittype = new Fittingtype();
		fittype.setBasetypeId(basetypeId);
		fittype.setLangId(langId);
		fittype.setName(fittingTypeName);
		fittype.setId(fittingTypeId);
		fittingService.updateFittingType(fittype);		
		return this.SUCCESS;
	}
	public String fetchFittingTypeByBaseId() throws Exception {
		fittingTypeList = fittingService.getFittingTypeByBaseId(fittingtypebaseIdQ);
		fittingtypebaseIdQ =-1;
		return this.SUCCESS;
	}
}
